ENT.Type = "point"
ENT.DM_Door = nil
ENT.TDM_Door = nil
ENT.Arena_Door = nil
ENT.CTF_Door = nil

RPGDM_StartedCountdown = false
RPGDM_GameType = "hub"
RPGDM_NextMap = ""
RPGDM_NextType = ""
RPGDM_MapIcon = ""
ENT.LastGameType = "ctf"

ENT.PlayerCount = 0

DM_V= 0
TDM_V= 0
Arena_V= 0
CTF_V= 0

DM_MAPS = {
"rpgdm_org_arena"
}
TDM_MAPS = {
"rpgdm_org_arena"
}
Arena_MAPS = {
"rpgdm_org_arena"
}
CTF_MAPS = {
"rpgdm_org_arena"
}

function ENT:Initialize()
	print("QAADADADADADADADAD")

	self.DM_Door = ents.FindByName("DMDOOR")
	self.TDM_Door = ents.FindByName("TDMDOOR")
	self.Arena_Door = ents.FindByName("ARENADOOR")
	self.CTF_Door = ents.FindByName("CTFDOOR")
	
	if(file.Exists("data", "rpgdm/currentgametype.txt"))then
		self.LastGameType = file.Read("rpgdm/currentgametype.txt")
	else
		file.Write("rpgdm/currentgametype.txt", self.LastGameType)
	end
	
	if(self.LastGameType == "ctf")then
		for k, v in pairs(self.CTF_Door)do
			v:Fire("Close")
		end
	elseif(self.LastGameType == "tdm")then
		for k, v in pairs(self.TDM_Door)do
			v:Fire("Close")
		end
	elseif(self.LastGameType == "arena")then
		for k, v in pairs(self.Arena_Door)do
			v:Fire("Close")
		end
	elseif (self.LastGameType == "	dm")then
		for k, v in pairs(self.DM_Door)do
			v:Fire("Close")
		end
	end
end

function ENT:Think()
	self.PlayerCount = table.Count(player.GetAll())
	if(!RPGDM_StartedCountdown and self.PlayerCount > 0)then
		local TotalVotes = (CTF_V + TDM_V + Arena_V + DM_V)
		if(TotalVotes >= (self.PlayerCount/2))then
			RPGDM_StartedCountdown = true
			CreateVisualTimer(5, function() 
				if(CTF_V >= TotalVotes/2)then
					RPGDM_NextMap = CTF_MAPS[math.random(1, table.Count(CTF_MAPS))]
					RPGDM_NextType = "CTF"
					RPGDM_MapIcon = "devin/blackbetty/hudicons/ctf.jpg"
				elseif(TDM_V >= TotalVotes/2)then
					RPGDM_NextMap = TDM_MAPS[math.random(1, table.Count(TDM_MAPS))]
					RPGDM_NextType = "TDM"
					RPGDM_MapIcon = "devin/blackbetty/hudicons/tdm.jpg"
				elseif(DM_V >= TotalVotes/2)then
					RPGDM_NextMap = DM_MAPS[math.random(1, table.Count(DM_MAPS))]
					RPGDM_NextType = "DM"
					RPGDM_MapIcon = "devin/blackbetty/hudicons/dm.jpg"
				elseif(Arena_V >= TotalVotes/2)then
					RPGDM_NextMap = Arena_MAPS[math.random(1, table.Count(Arena_MAPS))]
					RPGDM_NextType = "Arena"
					RPGDM_MapIcon = "devin/blackbetty/hudicons/arena.jpg"
				end
				CreateVisualTimer(5, function()
					game.ConsoleCommand("changelevel".." "..RPGDM_NextMap.."\n")
				end, "Starting "..RPGDM_NextType.." at "..RPGDM_NextMap..".", ""..RPGDM_MapIcon.."")
			end, "Conducting gamemode vote.", "devin/blackbetty/hudicons/plus.png")
		end
	end
	self:NextThink( CurTime() + 2 )
end

function ENT:AcceptInput( name, activator, caller, data )
    if (name == "addCTF") then 
		CTF_V = CTF_V+ 1
    
	elseif (name == "subCTF") then 
		CTF_V = CTF_V- 1
    
	elseif (name == "addTDM") then 
		TDM_V = TDM_V+ 1
    
	elseif (name == "subTDM") then 
		TDM_V = TDM_V- 1
		
	elseif (name == "addDM") then 
		DM_V = DM_V+ 1
		
	elseif (name == "subDM") then 
		DM_V = DM_V- 1
    
	elseif (name == "addArena") then 
		Arena_V= Arena_V+ 1
  
	elseif (name == "subArena") then 
		Arena_V= Arena_V- 1
    end
end



